在java中如何设置本地的时间? Date nowdate=new Date();String date=nowdate.tolocalString(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Date nowdate=new Date();String date=nowdate.tolocalString(); Date类中不是有相应的set方法吗? 谢谢大家的捧场。to shihongkitty(leejin) & Leemaasn(我给大家拜早年啦!新春快乐!!!) 我是想设置,而不是想获取。 关注!ICQ:348732090Yahoo:huangxinshui2002 to nobodyelse(空无一人) date类的set方法只对本类有用,不能影响到本地机器时间。 采用备用方案解决了。用vc写了个小win32控制台程序。让后用java的runtime.getruntime().exec解决了。但是感觉很不是滋味,偶是个新手,希望有帮忙高手解决。 差不多应该可以直接调用WinAPI,这样你的控制台程序就可以省略了不过思路是相似的不过我没有用过jni,所以不知道怎么实现 ^_^ 最后的解决方法。是:Runtime.getRuntime().exec("C:\\WINNT\\system32\\cmd /c date 2003-1-17");不用vc写的小控制台程序了。如果还有更好的方法的话,请告知。 以下是使用JNI实现的代码:package systemtime;public class CppExtention{ private static native boolean cppSetSystemTime(int year,int month,int day,int hour,int minute,int second,int milliSecond); static { System.loadLibrary("CppExtention"); } public static boolean setSystemTime(int year,int month,int day,int hour,int minute,int second,int milliSecond) { return cppSetSystemTime(year,month,day,hour,minute,second,milliSecond); }}根据这个文件使用javah生成相关本地方法的头文件然后新建一个dll工程,实现相关本地方法,如下:// CppExtention.cpp : Defines the initialization routines for the DLL.///////////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "CppExtention.h"#include "systemtime_CppExtention.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// Message map/////////////////////////////////////////////////////////////////////////////BEGIN_MESSAGE_MAP(CCppExtentionApp, CWinApp) //{{AFX_MSG_MAP(CCppExtentionApp) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CCppExtentionApp construction/////////////////////////////////////////////////////////////////////////////CCppExtentionApp::CCppExtentionApp(){}/////////////////////////////////////////////////////////////////////////////// The one and only CCppExtentionApp object/////////////////////////////////////////////////////////////////////////////CCppExtentionApp g_cppExtentionApp;/////////////////////////////////////////////////////////////////////////////// The one and only CCppExtentionApp object/////////////////////////////////////////////////////////////////////////////JNIEXPORT jboolean JNICALL Java_systemtime_CppExtention_cppSetSystemTime(JNIEnv* pEnv,jclass jcls, jint year, jint month, jint day, jint hour,jint minute, jint second, jint milliSecond){ SYSTEMTIME sysTime; GetSystemTime(&sysTime); sysTime.wYear=(unsigned short)year; sysTime.wMonth=(unsigned short)month; sysTime.wDay=(unsigned short)day; sysTime.wHour=(unsigned short)hour; sysTime.wMinute=(unsigned short)minute; sysTime.wSecond=(unsigned short)second; sysTime.wMilliseconds=(unsigned short)milliSecond; FILETIME ft,ftLocal; SystemTimeToFileTime(&sysTime,&ftLocal); LocalFileTimeToFileTime(&ftLocal,&ft); FileTimeToSystemTime(&ft,&sysTime); return SetSystemTime(&sysTime);} 使用方法:package systemtime;public class SystemTime{ public static void main(String[] args) { System.out.println(CppExtention.setSystemTime(2004,1,18,9,49,0,0)); }} list里相同的问题 ssh集成问题 关于JAVA的封装的一些疑问 动态编译如何设定classpath? WINDOWS-延缓写入失败的问题 如何取出JTable中的第一列的乱码!!!在线等!!!答对即揭帖!!! 如何在java中实现文件拷贝 我是一个用delphi编程俩年的程序员,现在想学Jave应该从那里开始,有什么好的书籍适合我看,怎样快点入门 进程通信 请问怎样把null转换成String类型,具体见内 如何通过Class.getField得到类的静态属性 如何学习JAVA
String date=nowdate.tolocalString();
to shihongkitty(leejin) & Leemaasn(我给大家拜早年啦!新春快乐!!!)
我是想设置,而不是想获取。
ICQ:348732090
Yahoo:huangxinshui2002
date类的set方法只对本类有用,不能影响到本地机器时间。
用vc写了个小win32控制台程序。
让后用java的runtime.getruntime().exec解决了。
但是感觉很不是滋味,偶是个新手,
希望有帮忙高手解决。
应该可以直接调用WinAPI,这样你的控制台程序就可以省略了
不过思路是相似的
不过我没有用过jni,所以不知道怎么实现 ^_^
是:
Runtime.getRuntime().exec("C:\\WINNT\\system32\\cmd /c date 2003-1-17");
不用vc写的小控制台程序了。
如果还有更好的方法的话,请告知。
{
private static native boolean cppSetSystemTime(int year,int month,int day,int hour,int minute,int second,int milliSecond);
static
{
System.loadLibrary("CppExtention");
} public static boolean setSystemTime(int year,int month,int day,int hour,int minute,int second,int milliSecond)
{
return cppSetSystemTime(year,month,day,hour,minute,second,milliSecond);
}
}根据这个文件使用javah生成相关本地方法的头文件
然后新建一个dll工程,实现相关本地方法,如下:
// CppExtention.cpp : Defines the initialization routines for the DLL.
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CppExtention.h"#include "systemtime_CppExtention.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Message map
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CCppExtentionApp, CWinApp)
//{{AFX_MSG_MAP(CCppExtentionApp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCppExtentionApp construction
/////////////////////////////////////////////////////////////////////////////
CCppExtentionApp::CCppExtentionApp()
{
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CCppExtentionApp object
/////////////////////////////////////////////////////////////////////////////
CCppExtentionApp g_cppExtentionApp;
/////////////////////////////////////////////////////////////////////////////
// The one and only CCppExtentionApp object
/////////////////////////////////////////////////////////////////////////////
JNIEXPORT jboolean JNICALL Java_systemtime_CppExtention_cppSetSystemTime
(JNIEnv* pEnv,jclass jcls, jint year, jint month, jint day,
jint hour,jint minute, jint second, jint milliSecond)
{
SYSTEMTIME sysTime;
GetSystemTime(&sysTime); sysTime.wYear=(unsigned short)year;
sysTime.wMonth=(unsigned short)month;
sysTime.wDay=(unsigned short)day;
sysTime.wHour=(unsigned short)hour;
sysTime.wMinute=(unsigned short)minute;
sysTime.wSecond=(unsigned short)second;
sysTime.wMilliseconds=(unsigned short)milliSecond; FILETIME ft,ftLocal;
SystemTimeToFileTime(&sysTime,&ftLocal);
LocalFileTimeToFileTime(&ftLocal,&ft);
FileTimeToSystemTime(&ft,&sysTime);
return SetSystemTime(&sysTime);
}
package systemtime;public class SystemTime
{
public static void main(String[] args)
{
System.out.println(CppExtention.setSystemTime(2004,1,18,9,49,0,0));
}
}