繁体   English   中英

如何使用JGit设置提交时间?

[英]How to set the commit time with JGit?

有没有办法用JGit设置提交时间?

我浏览了API,发现它只能通过修改本地系统时间来完成。 我想通过代码实现它。 并且可以在win系统上正常运行。

可以使用CommitCommand设置提交的时间戳。 请注意,必须与PersonIdent对象一起指定名称,电子邮件和时间戳。

例如:

Date date = ...
PersonIdent defaultCommitter = new PersonIdent(git.getRepository());
PersonIdent committer = new PersonIdent(defaultCommitter, date);
git.commit().setMessage("Commit with time").setCommitter(committer).call();

defaultCommitter保存git config中定义的名称和电子邮件,时间戳是当前系统时间。 使用第二个PersonIdent构造函数,名称和电子邮件来自defaultCommitter ,时间戳被date覆盖。

在Windows中,可以通过从'Administrator'命令提示符执行命令'date MM-dd-yy'来设置系统时间。

适用于Windows的Java代码段

 //Set the Date 
 SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");  
 String setDate = "cmd /C date "+sdf.format(dateToSet);  
 Process dateProc = Runtime.getRuntime().exec(setDate);  
 dateProc.waitFor();//Might take a couple of seconds

 //Set the Time  
 SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");  
 String setTime = "cmd /C time "+stf.format(dateToSet);  
 Process timeProc = Runtime.getRuntime().exec(setTime);  
 timeProc.waitFor();//Might take a couple of seconds  

该命令只能以管理员身份执行。 因此,您应该使用管理员权限运行Java代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM